#! /bin/sh


#####################################
# USAGE:
# install_vcomponents_v4rb app_path [arch] [use_vkernel] [use_vclient] [use_vreports]
# install_vcomponents_v4rb My\ Application.app 64 1 0 0


#####################################
# READ PARAMS
#
app=$1
arch=$2

use_vkernel=$3
use_vclient=$4
use_vreports=$5


#####################################
# PREPARE variables:
#

# Check if we install vcomponents into app bundle
bundle=0
if [[ "$app" == *.app ]] ; then
	bundle=1
fi

# NOTE that "app"/Contents specify slash.
# this is not a problem even if app itself have slash at end.
# We will get something as  gg.app//Contents and this still works fine.
#
if [ "$arch" = "64" ] ; then
	sys_vcomp=/usr/local/lib/vcomponents_x64
	dllsuff="_x64"
else	
	sys_vcomp=/usr/local/lib/vcomponents
	dllsuff=""
fi

if [ $bundle = 1 ] ; then
	app_vcomp="$app/Contents/Frameworks/vcomponents$dllsuff"
	exec_vcomp="@executable_path/../Frameworks/vcomponents$dllsuff"
    app_resources="$app/Contents/Resources"
else
	app_vcomp=$(dirname "$app")/
	exec_vcomp="@executable_path"
    app_resources="$app_vcomp"
fi

install_vkernel=1
install_vclient=1
install_vreports=1

if [ "$use_vkernel" = "0" ] ; then
	install_vkernel=0
fi

if [ "$use_vclient" = "0" ] ; then
	install_vclient=0
fi

if [ "$use_vreports" = "0" ] ; then
	install_vreports=0
fi

if [ $install_vreports = 1 ] ; then
	install_vkernel=1
fi


#####################################
# COPY files of vcomponents into APP.
#####################################

if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

# VSHARED
if [ ! -e "$app_vcomp"/libvshared_fat_release$dllsuff.dylib ] ; then
	cp $sys_vcomp/libvshared_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VKERNEL
if [[ $install_vkernel = 1 && ! -e "$app_vcomp"/libvkernel_fat_release$dllsuff.dylib ]] ; then
	cp $sys_vcomp/libvkernel_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VCLIENT
if [[ $install_vclient = 1 && ! -e "$app_vcomp"/libvclient_fat_release$dllsuff.dylib ]] ; then
	cp $sys_vcomp/libvclient_fat_release$dllsuff.dylib 	"$app_vcomp"
fi

# VREPORT
if [[ $install_vreports = 1 && ! -e "$app_vcomp"/libvreport_fat_release$dllsuff.dylib ]] ; then
	cp $sys_vcomp/libvreport_fat_release$dllsuff.dylib 	"$app_vcomp"
fi


####################################
# Copy Folders:
####################################

if [ ! -d "$app_resources" ] ; then
	mkdir "$app_resources"
fi

# vresources
cp -R $sys_vcomp/vresources 					"$app_resources"


#####################################
# CHANGE executable_path for engine dylibs:
#####################################

	# change id of VSHARED
	install_name_tool -id "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" "$app_vcomp"/libvshared_fat_release$dllsuff.dylib

if [ $install_vkernel = 1 ] ; then

	# change id of VKERNEL
	install_name_tool -id "$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" "$app_vcomp"/libvkernel_fat_release$dllsuff.dylib

	install_name_tool -change $sys_vcomp/libvshared_fat_release$dllsuff.dylib   "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvkernel_fat_release$dllsuff.dylib
	install_name_tool -change $sys_vcomp/VSDK.framework/Versions/A/VSDK "$exec_vcomp/VSDK.framework/Versions/A/VSDK" 	"$app_vcomp"/libvkernel_fat_release$dllsuff.dylib

fi

if [ $install_vclient = 1 ] ; then

	# change id of VCLIENT
	install_name_tool -id "$exec_vcomp/libvclient_fat_release$dllsuff.dylib" "$app_vcomp"/libvclient_fat_release$dllsuff.dylib
	install_name_tool -change $sys_vcomp/libvshared_fat_release$dllsuff.dylib "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" "$app_vcomp"/libvclient_fat_release$dllsuff.dylib

fi

if [ $install_vreports = 1 ] ; then

	# change id of VREPORT
	install_name_tool -id "$exec_vcomp/libvreport_fat_release$dllsuff.dylib" "$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change $sys_vcomp/libvshared_fat_release$dllsuff.dylib  "$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change $sys_vcomp/libvkernel_fat_release$dllsuff.dylib  "$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib
	install_name_tool -change $sys_vcomp/libvclient_fat_release$dllsuff.dylib "$exec_vcomp/libvclient_fat_release$dllsuff.dylib" 	"$app_vcomp"/libvreport_fat_release$dllsuff.dylib

fi
	

#####################################
# now we need update yet App/Contents/Frameworks/V4RB.rbx_0_xxx.dylib
# Xojo(REALBasic) can assign any digits xxx, so we use globbing (regex) here to locate file.
# NOTE: we do cd into folder Frameworks to work with short files names and avoid absolute paths,
# which can have spaces, and this cause problem with install_name_tool.
#####################################

if [ $bundle = 1 ] ; then
	app_frmws="$app"/Contents/Frameworks
else
	app_frmws=$(dirname "$app")/$(basename "$app")\ Libs
fi

cd "$app_frmws"

for file in ./V4RB*
do
	v4rb_dylib="$file"
	break
done

if [ ! -e $v4rb_dylib ] ; then		# we try low case: at some point Xojo started to put v4rb_cocoa.dylib
	for file in ./v4rb*
	do
		v4rb_dylib="$file"
		break
	done
fi


#
# now we can change paths In the V4RB.dylib
#
install_name_tool -change $sys_vcomp/libvshared_fat_release$dllsuff.dylib 	"$exec_vcomp/libvshared_fat_release$dllsuff.dylib" 		"$v4rb_dylib"
install_name_tool -change $sys_vcomp/libvkernel_fat_release$dllsuff.dylib 	"$exec_vcomp/libvkernel_fat_release$dllsuff.dylib" 		"$v4rb_dylib"
install_name_tool -change $sys_vcomp/libvclient_fat_release$dllsuff.dylib 	"$exec_vcomp/libvclient_fat_release$dllsuff.dylib" 		"$v4rb_dylib"
install_name_tool -change $sys_vcomp/libvreport_fat_release$dllsuff.dylib 	"$exec_vcomp/libvreport_fat_release$dllsuff.dylib" 		"$v4rb_dylib"
